import { CheckboxGroupProvider } from './group-context'
import { usePropsValue } from '../../hooks/use-props-value'
import type { CheckboxGroupProps } from './type'

export const Group: React.FC<CheckboxGroupProps> = (props) => {
  const {
    disabled = false,
    defaultValue = [],
    onChange,
    value,
    children,
  } = props

  const [actValue, setActValue] = usePropsValue({
    value,
    defaultValue,
    onChange,
  })

  return (
    <CheckboxGroupProvider
      value={{
        value: actValue,
        disabled,
        check: (v) => {
          setActValue([...actValue, v])
        },
        uncheck: (v) => {
          setActValue(actValue.filter((item) => item !== v))
        },
      }}
    >
      {children}
    </CheckboxGroupProvider>
  )
}
